package com.kitchen.service.impl;

import com.kitchen.common.vo.ResultVO;
import com.kitchen.common.vo.ResStatus;
import com.kitchen.beans.entity.Category;
import com.kitchen.beans.vo.CategoryVO;
import com.kitchen.mapper.CategoryMapper;
import com.kitchen.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 78468
 * 商品分类接口实现
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CategoryService categoryService;


    /**
     * 显示所有分类
     * 除了热门分类
     *
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    @Override
    public ResultVO showAllProducts() {
        ResultVO resultVO = new ResultVO();


        try {
            /**
             * 查找一级分类
             */
            Example example = new Example(Category.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("cLevel", 1);
            List<Category> categories = categoryMapper.selectByExample(example);
            List<CategoryVO> transform = transform(categories);

            log.debug("查询到的一级商品为:{}", categories);

            /**
             * 根据上述的以查找每个一级分类相对应的二级分类
             * 并将二级分类赋值给一级分类
             */
            for (CategoryVO category : transform) {
                Example example1 = new Example(Category.class);
                Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("cBelongCategory", category.getCId());
                List<Category> categories1 = categoryMapper.selectByExample(example1);
                List<CategoryVO> transform1 = transform(categories1);

                /**
                 * 查找属于二级分类的三级分类
                 */
                for (CategoryVO categoryVO : transform1) {
                    Example example2 = new Example(Category.class);
                    Example.Criteria criteria2 = example2.createCriteria();
                    criteria2.andEqualTo("cBelongCategory", categoryVO.getCId());
                    List<Category> categories2 = categoryMapper.selectByExample(example2);
                    List<CategoryVO> transform2 = transform(categories2);

                    /**
                     * 查找三级分类相对应的四级分类
                     */
                    for (CategoryVO vo : transform2) {
                        Example example3 = new Example(Category.class);
                        Example.Criteria criteria3 = example3.createCriteria();
                        criteria3.andEqualTo("cBelongCategory", vo.getCId());

                        //将四级分类装配到三级分类
                        vo.setCategories(transform(categoryMapper.selectByExample(example3)));
                    }

                    //将三级分类给对应的二级分类
                    categoryVO.setCategories(transform2);
                }

                //将查询出来的二级分类放置到一级分类
                category.setCategories(transform1);

                log.debug("查询出来的属于一级分类的二级分类:", transform1);
            }

            //获取应该属于热门分类的12个二级分类
            List<CategoryVO> categoryVoS = categoryService.showPopularCategories();

            for (CategoryVO categoryVo : categoryVoS) {

                Example example1 = new Example(Category.class);
                Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("cBelongCategory", categoryVo.getCId());
                List<Category> categories1 = categoryMapper.selectByExample(example1);

                categoryVo.setCategories(transform(categories1));

            }

            for (CategoryVO categoryVO : transform) {
                //装配12个二级分类至[热门分类]
                if (categoryVO.getCId() == 1) {
                    categoryVO.setCategories(categoryVoS);
                } else if (categoryVO.getCId() == 2) {
                    //移除已经处于二级分类中的一级分类
                    List<CategoryVO> categories1 = categoryVO.getCategories();
                    List<CategoryVO> collect = categories1.stream().filter(categoryVo1 -> {
                        for (CategoryVO vo : categoryVoS) {
                            if (vo.getCId().equals(categoryVo1.getCId())) {
                                return false;
                            }
                        }
                        return true;
                    }).collect(Collectors.toList());
                    categoryVO.setCategories(collect);
                }
            }

            resultVO.setCode(ResStatus.SUCCESS);
            resultVO.setMsg(ResStatus.QUERY_ALL_PRODUCTS_SUCCESSFULLY);
            resultVO.setData(transform);
        } catch (Exception e) {
            resultVO.setCode(ResStatus.FAIL);
            resultVO.setMsg(ResStatus.FAILED_TO_QUERY_ALL_PRODUCTS);
        }
        return resultVO;
    }


    /**
     * 商品分类的热度
     * 在用户点击某个商品分类之后，对应的商品分类热度就会+1
     *
     * @param category 商品分类id
     * @return
     */
    @Transactional
    @Override
    public ResultVO increaseThePopularityOfGoods(Category category) {
        ResultVO resultVO = new ResultVO();
        final int LEVEL = 2;
        try {
            Category category1 = categoryMapper.selectByPrimaryKey(category);
            if (category1.getCLevel() == LEVEL) {
                category1.setCPopularity(category1.getCPopularity() + 1);
                log.debug("传入进来的商品分类的信息:{}", category1);

                int i = categoryMapper.updateByPrimaryKeySelective(category1);

                if (i > 0) {
                    resultVO.setCode(ResStatus.SUCCESS);
                    resultVO.setMsg(ResStatus.INCREASE_HEAT_SUCCESS);
                    resultVO.setData(i);
                } else {
                    resultVO.setCode(ResStatus.FAIL);
                    resultVO.setMsg(ResStatus.FAILED_TO_INCREASE_HEAT);
                }
                return resultVO;
            }
            resultVO.setCode(ResStatus.FAIL);
            resultVO.setMsg(ResStatus.NO_INCREASE_IN_HEAT);
        } catch (Exception e) {
            resultVO.setCode(ResStatus.FAIL);
            resultVO.setMsg(ResStatus.FAILED_TO_INCREASE_HEAT);
        }
        return resultVO;
    }


    /**
     * 热门分类
     *
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    @Override
    public List<CategoryVO> showPopularCategories() {

        List<Category> categories = categoryMapper.selectByCLevel();
        List<CategoryVO> transform = transform(categories);
        return transform;
    }


    /**
     * 将商品分类转换为商品分类vo
     *
     * @param categories 商品分类
     * @return
     */
    private List<CategoryVO> transform(List<Category> categories) {
        ArrayList<CategoryVO> categoryVOS = new ArrayList<>();
        for (Category category : categories) {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(category, categoryVO);
            categoryVOS.add(categoryVO);
        }
        return categoryVOS;
    }
}
